package com.hzit.controller;

import com.hzit.common.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;

	//1. 查询所有学生列表
	@GetMapping("/list")
	public R findAll(){
		List<Student> list = studentService.list();
		return R.ok().put("list",list);
	}

	//2. 添加学生
	@PostMapping("/add")
	public R add(@RequestBody StudentVo vo){
		Student student = new Student();
		BeanUtils.copyProperties(vo,student);
		studentService.save(student);
		return R.ok("添加学生成功！");
	}

	//3. 修改学生
	@PutMapping("/update")
	public R update(@RequestBody StudentVo vo){
		Student student = new Student();
		BeanUtils.copyProperties(vo,student);
		studentService.updateById(student);
		return R.ok("修改学生成功！");
	}

	//4. 删除学生
	@DeleteMapping("/delete/{id}")   // delete:  http://localhost:9001/student/delete/31
	public R delete(@PathVariable("id") Integer sid){
		studentService.removeById(sid);
		return R.ok("删除学生成功！");
	}
}
